package top.yokey.shopnc.activity.mine;

import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.LinearLayoutCompat;
import android.support.v7.widget.Toolbar;
import android.view.View;

import top.yokey.shopnc.view.PullRefreshView;
import top.yokey.shopnc.R;
import top.yokey.shopnc.adapter.GoodsBrowseListAdapter;
import top.yokey.shopnc.base.BaseApplication;
import top.yokey.base.base.BaseHttpListener;
import top.yokey.base.base.BaseSnackBar;
import top.yokey.shopnc.base.SwipeActivity;
import top.yokey.base.bean.BaseBean;
import top.yokey.base.bean.GoodsBrowseBean;
import top.yokey.base.model.MemberGoodsBrowseModel;
import top.yokey.base.util.JsonUtil;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;

/**
 * @author MapStory
 * @ qq 1002285057
 * @ Project https://gitee.com/MapStory/ShopNc-Android
 */

@ContentView(R.layout.activity_recycler_view)
public class FootprintActivity extends SwipeActivity {

    @ViewInject(R.id.mainLinearLayout)
    private LinearLayoutCompat mainLinearLayout;
    @ViewInject(R.id.mainToolbar)
    private Toolbar mainToolbar;
    @ViewInject(R.id.toolbarImageView)
    private AppCompatImageView toolbarImageView;

    @ViewInject(R.id.mainPullRefreshView)
    private PullRefreshView mainPullRefreshView;

    private int pageInt;
    private GoodsBrowseListAdapter mainAdapter;
    private ArrayList<GoodsBrowseBean> mainArrayList;

    @Override
    public void initData() {

        setToolbar(mainToolbar, "我的足迹");
        toolbarImageView.setImageResource(R.drawable.ic_action_delete);

        pageInt = 1;
        mainArrayList = new ArrayList<>();
        mainAdapter = new GoodsBrowseListAdapter(mainArrayList);
        mainPullRefreshView.getRecyclerView().setAdapter(mainAdapter);

        getGoodsBrowse();

    }

    @Override
    public void initEven() {

        toolbarImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                clearAll();
            }
        });

        mainPullRefreshView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mainPullRefreshView.isFailure()) {
                    pageInt = 1;
                    getGoodsBrowse();
                }
            }
        });

        mainPullRefreshView.setOnRefreshListener(new PullRefreshView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                pageInt = 1;
                getGoodsBrowse();
            }

            @Override
            public void onLoadMore() {
                getGoodsBrowse();
            }
        });

        mainAdapter.setOnItemClickListener(new GoodsBrowseListAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position, GoodsBrowseBean goodsBrowseBean) {
                BaseApplication.get().startGoods(getActivity(), goodsBrowseBean.getGoodsId());
            }
        });

    }

    //自定义方法

    private void clearAll() {

        toolbarImageView.setEnabled(false);
        BaseSnackBar.get().showHandler(mainLinearLayout);

        MemberGoodsBrowseModel.get().browseClearAll(new BaseHttpListener() {
            @Override
            public void onSuccess(BaseBean baseBean) {
                toolbarImageView.setEnabled(true);
                if (JsonUtil.isSuccess(baseBean.getDatas())) {
                    pageInt = 1;
                    getGoodsBrowse();
                } else {
                    BaseSnackBar.get().showFailure(mainLinearLayout);
                }
            }

            @Override
            public void onFailure(String reason) {
                toolbarImageView.setEnabled(true);
                BaseSnackBar.get().show(mainLinearLayout, reason);
            }
        });

    }

    private void getGoodsBrowse() {

        mainPullRefreshView.setLoading();

        MemberGoodsBrowseModel.get().browseList(pageInt + "", new BaseHttpListener() {
            @Override
            public void onSuccess(BaseBean baseBean) {
                if (pageInt == 1) {
                    mainArrayList.clear();
                }
                if (pageInt <= baseBean.getPageTotal()) {
                    String data = JsonUtil.getDatasString(baseBean.getDatas(), "goodsbrowse_list");
                    mainArrayList.addAll(JsonUtil.json2ArrayList(data, GoodsBrowseBean.class));
                    pageInt++;
                }
                mainPullRefreshView.setComplete();
            }

            @Override
            public void onFailure(String reason) {
                mainPullRefreshView.setFailure();
            }
        });

    }

}
